package biz

import (
	"context"
	"time"

	"github.com/go-kratos/kratos/v2/log"
)

type UserUseCase struct {
	ropo UserRepo
	log  *log.Helper
}

func NewUserUseCase(ropo UserRepo, logger log.Logger) *UserUseCase {
	return &UserUseCase{ropo: ropo, log: log.NewHelper(logger)}
}

type Users struct {
	Id        uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	Name      string    `gorm:"column:name;type:varchar(255);default:NULL;" json:"name"`
	Password  string    `gorm:"column:password;type:varchar(255);default:NULL;" json:"password"`
	CreatedAt time.Time `gorm:"column:created_at;type:datetime;default:CURRENT_TIMESTAMP;" json:"created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;default:CURRENT_TIMESTAMP;" json:"updated_at"`
	DeletedAt time.Time `gorm:"column:deleted_at;type:datetime;default:NULL;" json:"deleted_at"`
}

type UserRepo interface {
	CreateUser(cxt context.Context, model *Users) (uint, error)
	LoginUser(cxt context.Context, model *Users) (uint, error)
	ListUser(cxt context.Context, page int64, size int64) (*[]Users, error)
}

func (c *UserUseCase) CreateUser(ctx context.Context, username string, password string) (uint, error) {
	u := &Users{

		Name:     username,
		Password: password,
	}
	return c.ropo.CreateUser(ctx, u)
}

func (c *UserUseCase) LoginUser(ctx context.Context, username string, password string) (uint, error) {
	u := &Users{

		Name:     username,
		Password: password,
	}
	return c.ropo.LoginUser(ctx, u)
}
func (c *UserUseCase) ListUser(ctx context.Context, page int64, size int64) (*[]Users, error) {
	user, err := c.ropo.ListUser(ctx, page, size)
	if err != nil {
		return nil, err
	}
	return user, nil

}
